//--------------------------------------------------------------------------
// $Id: MemoryAssociationSessionManager.java 8 2006-08-21 10:19:47Z zendragon $
//--------------------------------------------------------------------------
// Copyright 2006 Social Brain Foundation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//--------------------------------------------------------------------------

package org.socialbrain.openid.consumer;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * @author <a href="http://erning.net/">Zhang Erning</a>
 * @version $Id: MemoryAssociationSessionManager.java 8 2006-08-21 10:19:47Z zendragon $
 */
public class MemoryAssociationSessionManager implements AssociationSessionManager
{
    public void put(String identity, AssociationSession session)
    {
        synchronized (_locker)
        {
            putSession(identity, session);
        }
    }

    public AssociationSession lookupByEndpoint(String endpoint)
    {
        synchronized (_locker)
        {
            AssociationSession session = _endpointSessionMap.get(endpoint);
            if (session != null && session.isExpired())
            {
                removeSession(session);
                return null;
            }
            return session;
        }
    }

    public AssociationSession lookupByIdentity(String identity)
    {
        AssociationSession session = _identitySessionMap.get(identity);
        if (session != null && session.isExpired())
        {
            removeSession(session);
        }
        return session;
    }

    private void putSession(String identity, AssociationSession session)
    {
        String endpoint = session.getEndpoint();
        _endpointSessionMap.put(endpoint, session);
        _identitySessionMap.put(identity, session);

        Set<String> set = _endpointIdentityMap.get(endpoint);
        if (set == null)
        {
            set = new HashSet<String>();
        }
        set.add(identity);
        _endpointIdentityMap.put(endpoint, set);
    }

    private void removeSession(AssociationSession session)
    {
        String endpoint = session.getEndpoint();

        Set<String> set = _endpointIdentityMap.get(endpoint);
        if (set != null)
        {
            for (String identity : set)
            {
                _identitySessionMap.remove(identity);
            }
        }
        _endpointIdentityMap.remove(endpoint);
        _endpointSessionMap.remove(endpoint);
    }

    private final Object _locker = new Object();

    private Map<String, AssociationSession> _endpointSessionMap = new HashMap<String, AssociationSession>();
    private Map<String, AssociationSession> _identitySessionMap = new HashMap<String, AssociationSession>();
    private Map<String, Set<String>> _endpointIdentityMap = new HashMap<String, Set<String>>();

    private transient static final Log _logger = LogFactory.getLog(MemoryAssociationSessionManager.class);
}
